package com.luoying.itew.view;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luoying.itew.R;
import com.luoying.itew.activity.PhotoPreviewActivity;
import com.luoying.itew.bean.InspectLogBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/5/25.
 * time 2018-5-10
 * author zm
 */
public class InspectLogItemView extends RelativeLayout {
    @BindView(R.id.tv_log_name)
    TextView tvLogName;
    @BindView(R.id.tv_log_createtime)
    TextView tvLogCreatetime;

    @BindView(R.id.tv_log_buildingState)
    TextView tvLogBuildingState;
    @BindView(R.id.tv_log_monitorState)
    TextView tvLogMonitorState;
    @BindView(R.id.tv_log_roadState)
    TextView tvLogRoadState;
    @BindView(R.id.tv_log_address)
    TextView tvLogAddress;
    @BindView(R.id.tv_submit_content)
    TextView tvSubmitContent;

    @BindView(R.id.gv_photo)
    NewGridView gvPhoto;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    public InspectLogItemView(Context context) {
        this(context, null);
    }

    public InspectLogItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.log_itemview, this);
        ButterKnife.bind(this, this);
    }

    class MyAdapter extends BaseAdapter {
        //上下文对象
        private Context context;
        private ArrayList<String> mlist;

        MyAdapter(Context context, ArrayList<String> list) {
            this.context = context;
            this.mlist = list;
        }

        @Override
        public int getCount() {
            return mlist.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, final View convertView, ViewGroup parent) {
            final SeeItemView venuesDataItemView;

            if (convertView == null) {

                venuesDataItemView = new SeeItemView(context);

            } else {
                venuesDataItemView = (SeeItemView) convertView;
            }
            venuesDataItemView.setData(mlist.get(position));
            venuesDataItemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    previewPhoto(mlist.get(position));
                }
            });
            return venuesDataItemView;
        }
    }

    private void previewPhoto(String path){
        Intent intent = new Intent(getContext(), PhotoPreviewActivity.class);
        intent.putExtra("from",1);
        intent.putExtra("path",path);
        getContext().startActivity(intent);
    }

    public void setData(InspectLogBean.PaginationBean.RecordsBean recordsBean) {
        String walkerName = recordsBean.getWalkerName();
        String logCreateTime = recordsBean.getLogCreateTime();
        String buildingState = recordsBean.getBuildingState();
        String monitorState = recordsBean.getMonitorState();
        String roadState = recordsBean.getRoadState();
        String walkerLocation = recordsBean.getWalkerLocation();
        String logContent = recordsBean.getLogContent();
        ArrayList<String> imgArr = (ArrayList<String>) recordsBean.getImgArr();
        if (!TextUtils.isEmpty(logContent)){
            tvSubmitContent.setText(logContent);
        }
        if (!TextUtils.isEmpty(walkerName)) {
            tvLogName.setText(walkerName);
        }
        tvLogCreatetime.setText(logCreateTime);
        if (!TextUtils.isEmpty(buildingState)) {
            tvLogBuildingState.setText(buildingState);
        }
        if (!TextUtils.isEmpty(monitorState)) {
            tvLogMonitorState.setText(monitorState);
        }
        if (!TextUtils.isEmpty(roadState)) {
            tvLogRoadState.setText(roadState);
        }
        if (!TextUtils.isEmpty(walkerLocation)) {
            tvLogAddress.setText(walkerLocation);
        }
        if (imgArr!=null && imgArr.size()!=0){
            MyAdapter myAdapter = new MyAdapter(getContext(), imgArr);
            gvPhoto.setAdapter(myAdapter);
            myAdapter.notifyDataSetChanged();
        }else {
            gvPhoto.setVisibility(GONE);
        }
    }
}
